package com.hualan.web.fore;

import com.hualan.pojo.Problems;
import com.hualan.service.service.service.ProblemService;
import com.hualan.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/problem")
public class ProblemController {
    @Autowired
    private ProblemService problemService;
    @RequestMapping("/listAll")
    public ResultEntity listAll(Integer count) {
        List<Problems> list = problemService.listAll();
        System.out.println(list.toString());
        if(count != null) {
            System.out.println("接收到count===============");
            list = list.stream().sorted((p1,p2) -> p2.getCount() - p1.getCount()).collect(Collectors.toList());
            System.out.println(list);
            return ResultEntity.ok(list);
        }
        list = list.stream().sorted((p1, p2) -> p2.getTime().compareTo(p1.getTime())).collect(Collectors.toList());

        return ResultEntity.ok(list);
    }
    @RequestMapping("/listById")
    public ResultEntity listById(Integer pid) {
        return ResultEntity.ok(problemService.listByPid(pid));
    }
    @RequestMapping("/add")
    public ResultEntity add(@RequestBody Problems problem) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        problem.setTime(dateFormat.format(new Date()));
        return ResultEntity.ok(problemService.add(problem));
    }
}
